﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Card.Manager
{
    public partial class AreaEdit : BasePage
    {
        #region 属性
        private int ParentId
        {
            get
            {
                if (Request.QueryString["parentId"] != null)
                {
                    return int.Parse(Request.QueryString["parentId"].ToString());
                }
                else
                {
                    return 0;
                }
            }
        }
        private int AreaId
        {
            get
            {
                if (Request.QueryString["ID"] != null)
                {
                    return Convert.ToInt32(Request.QueryString["ID"]);
                }
                else
                {
                    return 0;
                }
            }
        }
        #endregion

        private string GetAreaType(string areaType)
        {
            string levelName = "";
            switch (areaType)
            {
                case "1":
                    levelName = "省/直辖市";
                    break;
                case "2":
                    levelName = "市";
                    break;
                case "3":
                    levelName = "县/区";
                    break;
            }
            return levelName;
        }

        protected override void OnInit(EventArgs e)
        {
            this.dropAreaType.SelectedIndexChanged += new EventHandler(dropAreaType_SelectedIndexChanged);
            base.OnInit(e);
        }

        void dropAreaType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.dropAreaType.SelectedValue == "3")
            {
                this.txtSiteCode.Enabled = true;
                this.dropIsActive.Enabled = true;
            }
            this.DoInitDropParentArea((int.Parse(this.dropAreaType.SelectedValue) - 1).ToString());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DoInit();
            }
        }

        protected void DoInit()
        {
            if (AreaId != 0)
            {
                Entity.Area area = BLL.AreaManager.GetEntity(AreaId, this.CPassport);
                Card.Utility.ControlUtility.SelectItemByValue(this.dropAreaType, area.AreaType);
                this.DoInitDropParentArea((int.Parse(area.AreaType) - 1).ToString());
                Card.Utility.ControlUtility.SelectItemByValue(this.dropParent, area.ParentAreaID.ToString());
                this.txtName.Text = area.Name;
                this.txtSort.Text = area.Sort.ToString();
                
                if (area.AreaType == "3")
                {
                    this.txtSiteCode.Text = area.SiteCode;
                    this.txtURL.Text = area.SiteUrl;
                    Card.Utility.ControlUtility.SelectItemByValue(this.dropIsActive, area.IsActive);
                    this.txtURL.Enabled = true;
                    this.txtSiteCode.Enabled = true;
                    this.dropIsActive.Enabled = true;
                }
                this.dropAreaType.Enabled = true;
                this.dropParent.Enabled = true;
                InitRep(area.ParentAreaID);                
            }
            else
            {
                if (ParentId == 0)
                {
                    Card.Utility.ControlUtility.SelectItemByValue(this.dropAreaType, "1");
                    this.DoInitDropParentArea("0");
                    Card.Utility.ControlUtility.SelectItemByValue(this.dropParent, "0");
                }
                else
                {
                    Entity.Area area = BLL.AreaManager.GetEntity(ParentId, this.CPassport);                    
                    Card.Utility.ControlUtility.SelectItemByValue(this.dropAreaType, (int.Parse(area.AreaType) + 1).ToString());
                    this.dropParent.Items.Clear();
                    this.dropParent.Items.Add(new ListItem(area.Name, area.ID.ToString()));
                    Card.Utility.ControlUtility.SelectItemByValue(this.dropParent, area.ID.ToString());                    
                    if (int.Parse(area.AreaType) + 1 == 3)
                    {
                        this.txtURL.Enabled = true;
                        this.txtSiteCode.Enabled = true;
                        this.dropIsActive.Enabled = true;
                    }
                }
                InitRep(ParentId);
            }
        }

        private void DoInitDropParentArea(string areaTpye)
        {
            this.dropParent.Items.Clear();
            if (areaTpye != "0")
            {
                this.dropParent.DataSource = BLL.AreaManager.GetList(string.Format(" AND AREA_TYPE='{0}'", areaTpye),null).Tables[0];
                this.dropParent.DataTextField = "NAME";
                this.dropParent.DataValueField = "ID";
                this.dropParent.DataBind();
            }
            else
            {
                this.dropParent.Items.Add(new ListItem("中国", "0"));
            }
        }

        protected void InitRep(int parId)
        {
            this.rptList.DataSource = BLL.AreaManager.GetListByParentID(parId, this.CPassport);
            this.rptList.DataBind();
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            Entity.Area area = new Entity.Area();
            if (AreaId != 0)
            {
                area = BLL.AreaManager.GetEntity(AreaId, this.CPassport);
            }
            area.Name = this.txtName.Text;
            area.ParentAreaID = int.Parse(this.dropParent.SelectedValue);
            area.Sort = int.Parse(this.txtSort.Text);
            area.AreaType = this.dropAreaType.SelectedValue;
            if (area.AreaType == "3")
            {
                area.SiteCode = this.txtSiteCode.Text;
                area.SiteUrl = this.txtURL.Text;
                area.IsActive = this.dropIsActive.SelectedValue;
                if (string.Compare(area.IsActive, CONST.YES_NO.YES) == 0)
                {
                    if (string.IsNullOrEmpty(area.SiteCode) || string.IsNullOrEmpty(this.txtURL.Text))
                    {
                        this.MessageBox("要开通分站请填写分站代码和分站地址！");
                        return;
                    }

                    if (BLL.AreaManager.ExistsSiteCode(area.SiteCode,area.ID, this.CPassport))
                    {
                        this.MessageBox("该分站代码已经被占用！");
                        return;
                    }
                }
            }
           
            BLL.AreaManager.Save(area, this.CPassport);
            this.MessageBox("保存成功！");
        }

        protected void btnOrder_Click(object sender, EventArgs e)
        {
            if (Request.Form["ckXuanze"] != null)
            {
                string[] ckIds = Request.Form["ckXuanze"].Split(',');
                for (int i = 0; i < ckIds.Length; i++)
                {
                    string strID = ckIds[i];
                    Entity.Area area = BLL.AreaManager.GetEntity(int.Parse(strID), this.CPassport);
                    string sort = Request.Form["txtSort" + strID];
                    if (sort != null && Utility.ValidationHelper.IsNumber(sort))
                    {
                        area.Sort = int.Parse(sort);
                        BLL.AreaManager.Save(area, this.CPassport);
                    }
                }
            }
            DoInit();
            this.MessageBox("修改成功！");
        }
    }
}
